home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / amiga / cmplibsr.zoo / $alloctvars1.P next >
Text File  |  1988-09-15  |  6KB  |  156 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona,1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25. /* **********************************************************************
  26. $alloctvars1_export([$alloc_tvars/3]).
  27.  
  28. $alloctvars1_use($computil1,[_,_,_,_,$max/3,_,_,_,_,_,_,
  29.                $check_type/2,_,_,_,$misc/2,_,_,_,_]).
  30. $alloctvars1_use($blist,[$append/3,$member/2,$member1/2]).
  31. $alloctvars1_use($listutil1,[_,_,_,_,_,_,$member2/2,_]).
  32. $alloctvars1_use($meta,[_,_,$length/2]).
  33. ********************************************************************** */
  34.  
  35.  
  36. $alloc_tvars([],_,_).
  37. $alloc_tvars([v(Vid,[o(M,Path,Lit,N,s,P)])|Vars],st(Clist,Vlist),LastGoals) :-
  38.     $check_type(P,t),
  39.     $misc(P,Misc), $member1(use(Use),Misc), $member1(nouse(NoUse),Misc),
  40.     $alloctvars_chkconflicts(Vid,Lit,M,[o(M,Path,Lit,N,s,P)],
  41.                         Clist,Vlist,Use,NoUse,LastGoals),
  42.     $alloc_tvars(Vars,st(Clist,Vlist),LastGoals).
  43. $alloc_tvars([v(Vid,[O1|ORest])|Vars],Syms,LastGoals) :-
  44.     $alloc1(Vid,O1,ORest,Syms,LastGoals),
  45.     $alloc_tvars(Vars,Syms,LastGoals).
  46.  
  47.  
  48. $alloctvars_chkconflicts(Vid,Lit0,M,Olist,Clist,Vlist,Use,NoUse,LastGoals) :-
  49.     ((Lit0 =:= 0, Lit1 = 1) ;    /* head */
  50.      (Lit0 =\= 0, Lit1 = Lit0)
  51.     ),
  52.     $alloc_get_first_noninline(Lit1,Clist,0,Nargs),
  53.     $alloctvars_chkconflicts1(Vid,Nargs,M,Lit1,
  54.                 Olist,Vlist,Use,NoUse,LastGoals).
  55.  
  56. $alloctvars_chkconflicts1(_,0,_,_,_,_,_,_,_).
  57. $alloctvars_chkconflicts1(Vid,N,M,Lit,Olist,Vlist,Use,NoUse,LastGoals) :-
  58.     $alloc_collect_lastgoals(Lit,LastGoals,ChLastGoals),
  59.     (($alloc_no_conflict(Vid,N,ChLastGoals),
  60.       $member2(o(_,_,Lit,N,t,_),Olist),
  61.       $member1(N, Use),
  62.       $alloc_request(M,N,Lit,VList)) ;
  63.      ($member1(N, NoUse))
  64.     ),
  65.     N1 is N - 1,
  66.     $alloctvars_chkconflicts1(Vid,N1,M,Lit,
  67.                 Olist,Vlist,Use,NoUse,LastGoals).
  68.  
  69.  
  70. $alloc_get_first_noninline(Lit,[],N,N).
  71. $alloc_get_first_noninline(Lit0,[c(Lit1,Nargs0,Prag)|CRest],Nargs1,Nargs) :-
  72.     ((Lit0 =:= Lit1,
  73.       ($member2(inline,Prag) ->
  74.         Nargs2 = Nargs1 ;
  75.         $max(Nargs0,Nargs1,Nargs2)
  76.       )
  77.      ) ;
  78.      (Lit0 =\= Lit1, Nargs2 = Nargs1)
  79.     ),
  80.     $alloc_get_first_noninline(Lit0,CRest,Nargs2,Nargs).
  81.  
  82.  
  83. /*   "$alloc_request" goes through the list of variable occurrences, and for
  84.       each variable in the head with an occurrence number less than or equal
  85.       to M, and requests that it not use register N, by adding the
  86.       pragma `nouse(N)'.                        */
  87.  
  88. :- index($alloc_request,4,4).
  89.  
  90. $alloc_request(_,_,_,[]).
  91. $alloc_request(M,N,Lit,[v(Vid,Occlist)|VRest]) :-
  92.     $alloc_request1(M,N,Lit,Occlist),
  93.     $alloc_request(M,N,Lit,VRest).
  94.  
  95.  
  96. $alloc_request1(_,_,_,X) :- var(X).
  97. $alloc_request1(M,_,Lit,[o(M1,Path,L,_,_,_)|_]) :-
  98.     ((M1 >= M) ; (L =\= Lit, L =\= Lit - 1)).
  99. $alloc_request1(M,N,Lit,[o(M1,Path,L,_,_,P)|ORest]) :-
  100.     M1 < M,
  101.     ((L is Lit) ; (L is Lit - 1)),
  102.     $misc(P,Misc), 
  103.     (($member1(use(Uselist),Misc), $member2(N,Uselist)) ;
  104.      ($member1(nouse(Nouselist),Misc), $member1(N,Nouselist))
  105.     ).
  106. $alloc_request1(_,_,_,[o(_,_,_,_,_,P)|_]) :- not($check_type(P,t)).
  107.  
  108. /* "$alloc1" does the first phase of register allocation for temporary
  109.     variables.  It adds `use(N)' pragmas to guide usage of registers at
  110.     the actual allocation time, and also adds `nouse(N)' pragmas to
  111.     indicate which registers should not be used at some point.        */
  112.  
  113. $alloc1(_,_,[],_,_).
  114. $alloc1(Vid,O1, OList, st(_,Vlist),LastGoals) :-
  115.     O1 = o(M,0,0,N,t,P1),
  116.     $check_type(P1,t),
  117.     $alloc_collect_lastgoals(1,LastGoals,ChLastGoals),
  118.     $alloc_no_conflict(Vid,N,ChLastGoals),
  119.     $member1(o(_,_,1,N,t,P2),OList),        /* args in same place    */
  120.     $misc(P1,Misc), $member1(use(L1),Misc),
  121.     $member1(N,L1),
  122.     $alloc_request(M,N,0,Vlist),
  123.     OList = [_|ORest],
  124.     $alloc1(Vid,O1, ORest, Vlist,LastGoals).
  125. $alloc1(Vid,o(M,Path,Lit,M1,_,P1),OList, st(Clist,Vlist),LastGoals) :-
  126.     $check_type(P1,t), $misc(P1,Misc),
  127.     $member1(use(Use),Misc), $member1(nouse(NoUse),Misc),
  128.     $alloctvars_chkconflicts(Vid,Lit,M,OList,Clist,Vlist,Use,NoUse,LastGoals).
  129. $alloc1(_,_,_,_,_).
  130.  
  131.  
  132. $alloc_collect_lastgoals(Chunk,[],[]).
  133. $alloc_collect_lastgoals(Chunk,[chunk(Chunk0,Call)|Rest],ChLastGoals) :-
  134.     ((Chunk =:= Chunk0, ChLastGoals = [Call|ChRest]) ;
  135.      (Chunk =\= Chunk0, ChLastGoals = ChRest)
  136.     ),
  137.     $alloc_collect_lastgoals(Chunk,Rest,ChRest).
  138.  
  139.  
  140. $alloc_no_conflict(Vid,N,[]).
  141. $alloc_no_conflict(Vid,N,['_call'(P,Args,_)|CRest]) :-
  142.     $length(Args,Arity),
  143.     ((Arity < N ) ;
  144.      (Arity >= N, $alloc_check_args(Args,N,Vid))
  145.     ),
  146.     $alloc_no_conflict(Vid,N,CRest).
  147.  
  148. $alloc_check_args([v(Vid,_)|_],1,Vid).
  149. $alloc_check_args([_|Args],N,Vid) :-
  150.     N > 1,
  151.     N1 is N-1,
  152.     $alloc_check_args(Args,N1,Vid).
  153.  
  154. /* ---------------------------- $alloctvars1.P ---------------------------- */
  155.  
  156.